﻿using System;
using System.IO;
using Mono.Cecil;

namespace CILADA.Core
{
    public static class TypeUtilities
    {
        public static bool TryResolveMethodReference(this MethodReference methodRef, out MethodDefinition resolvedMethodDef)
        {
            try
            {
                resolvedMethodDef = methodRef.Resolve();

                if (resolvedMethodDef == null)
                {
                    return false;
                }

                return true;
            }
            catch (FileNotFoundException fnfe)
            {
                // log error somewhere
            }
            catch (Exception e)
            {
                // log error somewhere
            }

            resolvedMethodDef = null;
            return false;
        }

        public static bool TryResolveTypeReference(this TypeReference typeRef, out TypeDefinition resolvedTypeDef)
        {
            try
            {
                resolvedTypeDef = typeRef.Resolve();

                if (resolvedTypeDef == null)
                {
                    return false;
                }

                return true;
            }
            catch (FileNotFoundException fnfe)
            {
                // log error somewhere
            }
            catch (Exception e)
            {
                // log error somewhere
            }

            resolvedTypeDef = null;
            return false;
        }
    }
}
